
#include "pows_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"

const uint32_t BLOCK_SIZE = 32;
const uint32_t BUFFER_NUM = 2;
const uint32_t TILE_NUM = 8;

namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

  PowsTilingData tiling;
  //获得处理器
  auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
  auto coreNum = ascendcPlatform.GetCoreNum();

  //获取输入数据的数量
  uint32_t inputNum = context->GetInputShape(0)->GetStorageShape().GetShapeSize();
  uint32_t typeBytes = 0;
  typeBytes = ge::GetSizeByDataType(context->GetInputDesc(0)->GetDataType());
  
  //获取输入数据总Bytes
  uint32_t inputLength = inputNum * typeBytes;  

  //UB
  uint64_t ubSize;
  ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize);
  uint32_t uB_BlockNum = (ubSize / BLOCK_SIZE / BUFFER_NUM);

  uint32_t tilingKey = 0,scale;
  ge::DataType dataType = context->GetInputDesc(0)->GetDataType();
  if (dataType == ge::DT_BF16){     //bf16
    scale = 8;
    tilingKey = 1;
  }else if(dataType == ge::DT_FLOAT16) {   //half & float
    scale = 8;
    tilingKey = 2;
  }else {
    scale = 8;
    tilingKey = 3;
  }
  uint32_t uselessBlock = uB_BlockNum % scale;
  uint32_t useBlock = uB_BlockNum - uselessBlock;
  uint32_t smallestVariableAllocBlock = useBlock / scale;

  uint32_t tileBlockNum = smallestVariableAllocBlock;
  uint32_t tileDataNum = (tileBlockNum * BLOCK_SIZE) / typeBytes;


  //输入数据32B向上对齐
  uint32_t inputLengthAlgin32 = (((inputLength + BLOCK_SIZE - 1) / BLOCK_SIZE) * BLOCK_SIZE);

  coreNum = (coreNum < inputLengthAlgin32 / BLOCK_SIZE) ? coreNum : inputLengthAlgin32 / BLOCK_SIZE;
  coreNum = (coreNum >= 1) ? coreNum : 1;

  uint32_t everyCoreInputBlockNum = inputLengthAlgin32 / BLOCK_SIZE / coreNum;
  uint32_t tailBlockNum = inputLengthAlgin32 / BLOCK_SIZE % coreNum;

  uint32_t smallCoreDataNum = everyCoreInputBlockNum * BLOCK_SIZE / typeBytes;   //小核需要处理的数据量
  uint32_t smallTileNum = everyCoreInputBlockNum / tileBlockNum;
  uint32_t finalSmallTileNum = (everyCoreInputBlockNum % tileBlockNum) == 0 ? smallTileNum : smallTileNum + 1;

  uint32_t smallTailDataNum = smallCoreDataNum - (tileDataNum * smallTileNum);
  smallTailDataNum = smallTailDataNum == 0 ? tileDataNum : smallTailDataNum;
  
  everyCoreInputBlockNum += 1;
  uint32_t bigCoreDataNum = everyCoreInputBlockNum * BLOCK_SIZE / typeBytes;
  uint32_t bigTileNum = everyCoreInputBlockNum / tileBlockNum;
  uint32_t finalBigTileNum = (everyCoreInputBlockNum % tileBlockNum) == 0 ? bigTileNum : bigTileNum + 1;
  uint32_t bigTailDataNum = bigCoreDataNum - (tileDataNum * bigTileNum);
  bigTailDataNum = bigTailDataNum == 0 ? tileDataNum : bigTailDataNum;

  tiling.set_size(inputNum);
  tiling.set_smallCoreDataNum(smallCoreDataNum);
  tiling.set_finalSmallTileNum(finalSmallTileNum);
  tiling.set_tileDataNum(tileDataNum);
  tiling.set_smallTailDataNum(smallTailDataNum);
  tiling.set_tailBlockNum(tailBlockNum);

  //广播BC
  uint32_t x1Length = context->GetInputTensor(0)->GetShapeSize();
  uint32_t x2Length = context->GetInputTensor(1)->GetShapeSize();
  uint32_t yLength = context->GetOutputShape(0)->GetStorageShape().GetShapeSize();

  if(yLength != x1Length || yLength != x2Length){
    int32_t y_dimNum, x1_dimNum, x2_dimNum;
    int32_t y_dims[20], x1_dims[20], x2_dims[20];
    int32_t y_sumDims[20], x1_sumDims[20], x2_sumDims[20];

    auto shape_y = context->GetOutputShape(0)->GetOriginShape();
    auto shape_x1 = context->GetInputShape(0)->GetOriginShape();
    auto shape_x2 = context->GetInputShape(1)->GetOriginShape();

    y_dimNum = shape_y.GetDimNum();
    x1_dimNum = shape_x1.GetDimNum();
    x2_dimNum = shape_x2.GetDimNum();

    for(int i=0; i<y_dimNum; i++){
        y_dims[y_dimNum-i-1] = shape_y.GetDim(i);

        if(i<x1_dimNum){
            x1_dims[x1_dimNum-i-1] = shape_x1.GetDim(i);
        }else{
            x1_dims[i] = 1;
        }

        if(i<x2_dimNum){
            x2_dims[x2_dimNum-i-1] = shape_x2.GetDim(i);
        }else{
            x2_dims[i]=1;
        }
    }

    y_sumDims[0] = 1;
    x1_sumDims[0] = 1;
    x2_sumDims[0] = 1;

    for(int i=1;i<=y_dimNum;i++){
        y_sumDims[i] = y_sumDims[i-1] * y_dims[i-1];
        x1_sumDims[i] = x1_sumDims[i-1] * x1_dims[i-1];
        x2_sumDims[i] = x2_sumDims[i-1] * x2_dims[i-1];
    }

    tiling.BCT.set_y_dimNum(y_dimNum);
    tiling.BCT.set_y_dims(y_dims);
    tiling.BCT.set_x1_dims(x1_dims);
    tiling.BCT.set_x2_dims(x2_dims);
    tiling.BCT.set_y_sumDims(y_sumDims);
    tiling.BCT.set_x1_sumDims(x1_sumDims);
    tiling.BCT.set_x2_sumDims(x2_sumDims);

    tilingKey = 4;   //广播Key
  }

  tiling.set_tilingKey(tilingKey);

  context->SetBlockDim(coreNum);
  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(),
                        context->GetRawTilingData()->GetCapacity());
  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
  size_t* currentWorkspace = context->GetWorkspaceSizes(1);
  currentWorkspace[0] = 0;
  return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class Pows : public OpDef {
public:
    explicit Pows(const char* name) : OpDef(name)
    {
        this->Input("x1")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_BF16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("x2")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_BF16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_BF16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(Pows);
}
